package com.tanhua.sso.controller;

import com.tanhua.sso.pojo.User;
import com.tanhua.sso.service.UserService;
import com.tanhua.sso.vo.ErrorResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author: JinboYang
 * @Description:
 * @Date: Created in 12:42 2021/1/2
 * @ModifiedBy:
 */
@RestController
@RequestMapping("user")
@Slf4j
public class UserController {

    //注入service
    @Autowired
    private UserService userService;

    /**
       * @Description:验证码校验 需要通过手机号和验证码一起校验
       * @Param:[param]
       * @return:org.springframework.http.ResponseEntity<java.lang.Object>
       * @Author:JinboYang
       * @Date:2021/1/2
       */
    @PostMapping("loginVerification")
    public ResponseEntity<Object> login(@RequestBody Map<String, String> param) {
        //获取手机的value
        String phone = param.get("phone");
        //获取验证码
        String code = param.get("verificationCode");
        //调用service中的login
        Map<String, Object> login = userService.login(phone, code);
        //如果login不为空说明登录成功
        if (login != null) {
            return ResponseEntity.ok(login);
        }
        //利用builder注解进行链式编程,给出登录失败的错误码和错误信息
        ErrorResult errorResult = ErrorResult.builder().errCode("000002").errMessage("登录失败").build();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

    /**
       * @Description: 校验token
       * @Param:[token]
       * @return:com.tanhua.sso.pojo.User
       * @Author:JinboYang
       * @Date:2021/1/3
       */
    @GetMapping("{token}")
    public User queryUserByToken(@PathVariable("token") String token){
        return userService.queryUserByToken(token);
    }
}
